package vip.xiaonuo.vein.impl.adapter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import vip.xiaonuo.facedoor.modular.memberdevice.entity.MemberDevice;
import vip.xiaonuo.facedoor.modular.memberuser.entity.MemberUser;
import vip.xiaonuo.facedoor.modular.memberuser.service.MemberUserService;
import vip.xiaonuo.facedoor.rule.VeinRulesEngine;
import vip.xiaonuo.facedoor.vo.dewo.*;

import java.util.List;
import java.util.Optional;

@Component
public class VeinMemberReqAdapter {


    @Autowired
    private MemberUserService memberUserService;

    public void deletePerson(MemberDevice memberDevice, String uri) throws Exception {
        DeletePersonRequest deletePersonRequest = new DeletePersonRequest();
        MemberUser byId = memberUserService.getById(memberDevice.getMemberId());
        deletePersonRequest.setMemberName(byId.getName());
        deletePersonRequest.setPersonnelId("1" + memberDevice.getMemberId().toString())
                .setMemberOrVisitorDeviceId(memberDevice.getId()).setMemberType(1);
        VeinRulesEngine.faceDoorDtoService.deletePerson(uri, deletePersonRequest);
    }


    public void deleteAllPerson(List<Long> memberIds, Long deviceId) throws Exception {
        DeletePersonRequest deletePersonRequest = new DeletePersonRequest();
        deletePersonRequest.setPersonnelIds(memberIds).setDeviceId(deviceId);
        VeinRulesEngine.faceDoorDtoService.deleteAllPerson(null, deletePersonRequest);
    }

    public void addPerson(String uri, MemberUser memberUser) throws Exception {
        AddPersonRequest addPersonRequest = new AddPersonRequest()
                .setPersonnelId("1" + memberUser.getId().toString()).setName(memberUser.getName())
                .setIdCardNum(memberUser.getIdCard()).setCompany(memberUser.getCompanyName())
                .setIcNum(memberUser.getIcCard()).setJobNum(memberUser.getJobNum())
                .setWiegandNum(memberUser.getWiegandNum())
                .setPersonnelType(Optional.ofNullable(memberUser.getPersonType()).orElse(0) + "")
                .setMemberOrVisitorDeviceId(memberUser.getMemberOrVisitorDeviceId()).setMemberType(memberUser.getMemberType());
        VeinRulesEngine.faceDoorDtoService.addPerson(uri, addPersonRequest);
    }

    public void updatePerson(String uri, MemberUser memberUser) throws Exception {
        UpdatePersonRequest updatePersonRequest = new UpdatePersonRequest();
        updatePersonRequest.setPersonnelId("1" + memberUser.getId().toString())
                .setName(memberUser.getName()).setIdCardNum(memberUser.getIdCard())
                .setCompany(memberUser.getCompanyName()).setIcNum(memberUser.getIcCard()).setJobNum(memberUser.getJobNum())
                .setWiegandNum(memberUser.getWiegandNum()).setMemberOrVisitorDeviceId(memberUser.getMemberOrVisitorDeviceId())
                .setMemberType(memberUser.getMemberType()).setPersonnelType(Optional.ofNullable(memberUser.getPersonType()).orElse(0) + "");
        updatePersonRequest.setFaceFeatureImgPath(memberUser.getPhoto());
        VeinRulesEngine.faceDoorDtoService.updatePerson(uri, updatePersonRequest);
    }


}
